//
//  XYClassfySelectHeaderView.swift
//  XYReadBook
//
//  Created by fox on 2017/9/7.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit

/// 命名不是很规范,比如说,两个临时的btn
protocol XYClassfySelectHeaderViewDelega:NSObjectProtocol {
    //设置协议方法
    
    /// 切换一级 类型 好评 热门.连载完结
    ///
    /// - Parameter sender: 切换一级 类型 好评 热门.连载完结
    func classfySelectType(sender:UIButton,headView:XYClassfySelectHeaderView)
    
    /// 切换二级分类
    ///
    /// - Parameter sender: 例如,玄幻大陆,异世大陆,穿越
    func classfySelectTitle(sender:UIButton,headView:XYClassfySelectHeaderView)
}


class XYClassfySelectHeaderView: UIView {
    var oldsender = -1
    var oldtemp = -1
    weak var delegate:XYClassfySelectHeaderViewDelega?
    var btn = UIButton()
    var sender = UIButton()
    var titleScrollView = UIScrollView()
    var titles:[String] = [] {
        didSet {
             setSameTypeButtons(titles: titles)
        }
    }
    var titleBtns:[UIButton] = []
    var segTitles = ["新书","热度","口碑","完结"]
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    
    func setupUI() {
        for index in 0...segTitles.count - 1 {
            let btn = UIButton()
            btn.setTitle(segTitles[index], for: .normal)
            btn.setTitleColor(UIColor.black, for: .normal)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            if index == 0 {
            btn.setTitleColor(UIColor.red, for: .normal)
            }
            let btnW = 70 * (ScreenWidth / 375.0)
            let btnX = index * Int(btnW)
            btn.frame = CGRect.init(x: btnX, y: 12, width: Int(btnW), height: 16)
            btn.tag = index
            btn.addTarget(self, action: #selector(selectClassfy(sender:)), for: .touchUpInside)
            addSubview(btn)
            titleBtns.append(btn)
        }
    }
    
    
    /// 设置buuton根据传递过来的数据决定放多少个
    /// 里面设置切换背景Button颜色
    /// - Parameter count: 设置标题同类型的按钮,根据传递进来的数据决定
    func setSameTypeButtons(titles:[String]){
        if titles.count == 0 {
            return
        }
        
            sender.setTitle("全部", for: .normal)
            sender.setTitleColor(UIColor.red, for: .normal)
            sender.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            sender.addTarget(self, action: #selector(clickTitleChangeColor(sender:)), for: .touchDown)
            sender.tag = 0
        let btnW = 70 * (ScreenWidth / 375.0)
            sender.frame = CGRect.init(x: 0, y: 12, width: Int(btnW), height: 16)
            titleScrollView.addSubview(sender)
        
        for index in 0...titles.count - 1 {
            let btn = UIButton()
            btn.setTitleColor(UIColor.black, for: .normal)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            btn.addTarget(self, action: #selector(clickTitleChangeColor(sender:)), for: .touchDown)
            btn.setTitleColor(UIColor.black, for: .normal)
            btn.tag = index + 1
            btn.setTitle(titles[index], for: .normal)
            titleScrollView.addSubview(btn)
    
            
            let btnX = index * Int(btnW) + Int(btnW)
            btn.frame = CGRect.init(x: btnX, y: 12, width: Int(btnW), height: 16)
        }
        
        titleScrollView.frame = CGRect.init(x: 0, y: 44, width: ScreenWidth, height: 44)
        let contentWidth = Int(btnW) * (titles.count + 1)
        titleScrollView.contentSize = CGSize.init(width:contentWidth, height: 0)
        addSubview(titleScrollView)
       
    }
    
    
    /// 点击二级分类
    ///
    /// - Parameter sender: 二级分类button
    func clickTitleChangeColor(sender:UIButton) {
        if oldsender == sender.tag {
            XYLog(messsage: "重复点击")
            /// 重复点击
            return
        }
        oldsender = sender.tag
        sender.setTitleColor(UIColor.black, for: .normal)
        self.sender.setTitleColor(UIColor.black, for: .normal)
        sender.setTitleColor(UIColor.red, for: .normal)
        self.sender = sender
        delegate?.classfySelectTitle(sender: sender, headView: self)
    }
    
    
    /// ["新书","热度","口碑","完结"]
    /// 按钮下标对应如上
    /// - Parameter sender: 点击的btn
    func selectClassfy(sender:UIButton) {
        
        if oldtemp == sender.tag {
            XYLog(messsage: "重复点击")
            /// 重复点击
            return
        }
        oldtemp = sender.tag
        let btn = titleBtns.first!
        btn.setTitleColor(UIColor.black, for: .normal)
        self.btn.setTitleColor(UIColor.black, for: .normal)
        sender.setTitleColor(UIColor.red, for: .normal)
        self.btn = sender
        delegate?.classfySelectType(sender: sender, headView: self)
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}



// MARK: - 因为刀哥的全屏侧滑,手势,会跟UIScorollView右滑动的时候,有冲突!故此方法
extension XYClassfySelectHeaderView:UIGestureRecognizerDelegate {
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        XYLog(messsage: "方法进来了")
        let bool = touch.view?.isInViewHirerackyOf(view:titleScrollView)
        if bool == true{
            return false
        }else {
            return true
        }
    }
}

